﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using DTO;
using System.Data;
using AutoMapper;
using System.Data.Objects;
using System.Data.SqlClient;



namespace Data
{
   public class TheLoaiRepository
    {
       private static TheLoaiDTO MapTheLoaiEFToTheLoaiDTO(THELOAI theloaiEF)
       {
           Mapper.CreateMap<THELOAI, TheLoaiDTO>();
           TheLoaiDTO theloaiDTO = Mapper.Map<THELOAI, TheLoaiDTO>(theloaiEF);
           return theloaiDTO;
       }
       private static THELOAI MapTheLoaiDTOToTheLoaiEF(TheLoaiDTO theloaiDTO)
       {
           Mapper.CreateMap<TheLoaiDTO,THELOAI>();
           THELOAI theloaiEF = Mapper.Map<TheLoaiDTO,THELOAI>(theloaiDTO);
           return theloaiEF;
       }
        public static List<TheLoaiDTO> GetAllTheLoai()
        {
            List<TheLoaiDTO> list = new List<TheLoaiDTO>();
            try
            {
                using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
                {
                    var query = from theloai in context.THELOAIList
                                select theloai;
                    foreach (var theloaiEF in query)
                    {
                        TheLoaiDTO theloaidto = MapTheLoaiEFToTheLoaiDTO(theloaiEF);
                        list.Add(theloaidto);
                    }
                }
               
            }
            catch(System.Exception ex)
            {
                throw (ex);
            }
            return list;

        }
        public static int NumberID()
        {
            int number=0;
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                try
                {
                    THELOAI query = (from theloai in context.THELOAIList
                                select context.THELOAIList.Max()).Last();


                    number = query.MaTheLoai;

                }
                catch
                {
                    
                }
            }
            return number;
        }
        public static void Insert(TheLoaiDTO theloaidto)
        {
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                THELOAI theloaiEF = new THELOAI();
                theloaiEF = MapTheLoaiDTOToTheLoaiEF(theloaidto);
                context.AddToTHELOAIList(theloaiEF);
                
                try
                {

                    context.SaveChanges();

                }
                catch (Exception)
                {

                }
            }
        }
        public static void Update(TheLoaiDTO theloai)
        {

            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                
                EntityKey key = context.CreateEntityKey("THELOAIList", new THELOAI{MaTheLoai=theloai.MaTheLoai});
                THELOAI origTheLoai = context.GetObjectByKey(key) as THELOAI;
                
                context.Detach(origTheLoai);
                context.Attach(origTheLoai);


                THELOAI updatetheloai = MapTheLoaiDTOToTheLoaiEF(theloai);

                //context.ApplyPropertyChanges("TheLoaiSet", updatetheloai);
                context.ApplyCurrentValues<THELOAI>("THELOAIList", updatetheloai);
               
                try
                {
                    context.SaveChanges();
                    
                }
                catch(System.Exception)
                {

                  
                }
            }


        }
        public static bool Delete(int matheloai)
        {
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                THELOAI origtheloai =( from theloai in context.THELOAIList
                            where theloai.MaTheLoai == matheloai
                            select theloai).First();

                context.DeleteObject(origtheloai);
                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
    }
}
